import type { IAccount } from "@/types"
import syRequest from ".."
import { LOGIN_TOKEN } from "@/global/constants"
import { localCache } from "@/utils/cacheData"

export function accountLogin(account: IAccount) {
  return syRequest.post({
    withCredentials: true, // 允许凭证授权
    url: "/login",
    data: account,
    interceptors: {
      responseSuccessFn(res) {
        // console.log("单次响应成功", res.headers.token)
        // 本地缓存token
        // console.log(res.headers)

        if (res.data.code == 200) {
          const token = res.headers.token
          token && localCache.setCache(LOGIN_TOKEN, token)
        }
        return res
      }
    }
  })
}

export function userRegister(account: IAccount) {
  return syRequest.post({
    url: "/users/register",
    data: account
  })
}

/**
 * 测试鉴权
 * @returns
 */
export function authentication() {
  return syRequest.get({
    url: "/login/test"
  })
}
